package com.abel.test;

import java.util.Arrays;

/**
 * @author xiachao
 * @date 2018/12/23
 */
public class Main1 {
    public static void main(String[] args) {
        int [] arr = new int[] {2, 3, -2, 4, 2};

        System.out.println(new Main1().maxSub(arr));
    }

    public int [] firstAndLast(int [] arr, int target){
        int index = Arrays.binarySearch(arr, target);
        if(index < 0){
            return new int [] {-1, -1};
        }
        int [] ret = new int[2];
        ret[0] = index;
        if(arr.length - 1 > index && arr[index + 1] == target){
            ret[1] = index + 1;
        }else{
            ret[1] = index;
        }
        return ret;
    }

    public int maxSub(int [] arr){
        int last_max = arr[0];
        int last_min = arr[0];
        int result = arr[0];
        int cur_max;
        int cur_min;

        for(int i = 1; i < arr.length; i ++){
            cur_max = Integer.max(arr[i], Integer.max(last_max * arr[i], last_min * arr[i]));
            cur_min = Integer.min(arr[i], Integer.min(last_max * arr[i], last_min * arr[i]));
            result = Integer.max(result, cur_max);
            last_max = cur_max;
            last_min = cur_min;
        }
        return result;
    }

}
